﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using DeGiglio.biz.Twitter.Properties;
using userControls = DeGiglio.biz.Twitter.UserControls;
using DeGiglio.biz.Twitter.Resources;
using DeGiglio.biz.Twitter.Classes;

namespace DeGiglio.biz.Twitter.SharePoint.Webparts
{
    [ToolboxItemAttribute(false)]
    public class DisplayTweets : WebPart
    {
        protected override void CreateChildControls()
        {
            userControls.DisplayTweets control = Page.LoadControl(ProjectSettings.Default.ControlPath_DisplayTweets) as userControls.DisplayTweets;
            if (control != null)
            {
                control.DisplayTweetsWebpart = this;
                this.Controls.Add(control);
            }
        }

        private void ClearTweetCache()
        {
            TwitterHelper helper = new TwitterHelper(TwitterHelper.RequestType.ClearCache);
            helper.ScreenName = this.TwitterScreenName;
            if (!string.IsNullOrEmpty(this.TwitterListName))
            {
                helper.ListName = this.TwitterListName;
            }
            helper.ClearTweetCache();
            helper = null;
        }

        #region " Member variables for setting property default values "
        private int numberOfTweetsToDisplay = 5;
        private int cacheTimespan = ProjectSettings.Default.CacheTimespan_DefaultValue;
        private bool includeRetweets = true;
        private bool showAvatars = true;
        private bool showScreenNames = true;
        private bool loadAvatarsUsingSsl = false;

        private bool clearCache = false;

        private bool jqueryOnMasterPage = false;
        private bool jtemplateOnMasterPage = false;
        private bool jtimerOnMasterPage = false;
        #endregion

        #region " Twitter Properties "
        #region TwitterScreenName
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_TwitterScreenName"),
         DescriptionLocalized("WPPropertyDescription_TwitterScreenName")]
        public string TwitterScreenName { get; set; }
        #endregion

        #region TwitterListName
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_TwitterList"),
         DescriptionLocalized("WPPropertyDescription_TwitterList")]
        public string TwitterListName { get; set; }
        #endregion

        #region TwitterConsumerKey
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_ConsumerKey"),
         DescriptionLocalized("WPPropertyDescription_ConsumerKey")]
        public string TwitterConsumerKey { get; set; }
        #endregion

        #region TwitterConsumerSecret
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_ConsumerSecret"),
         DescriptionLocalized("WPPropertyDescription_ConsumerSecret")]
        public string TwitterConsumerSecret { get; set; }
        #endregion

        #region NumberOfTweetsToDisplay
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_NumberOfTweets"),
         DescriptionLocalized("WPPropertyDescription_NumberOfTweets")]
        public int NumberOfTweetsToDisplay
        {
            get { return this.numberOfTweetsToDisplay; }
            set { this.numberOfTweetsToDisplay = value; }
        }
        #endregion

        #region IncludeRetweets
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_IncludeRetweets"),
         DescriptionLocalized("WPPropertyDescription_IncludeRetweets")]
        public bool IncludeRetweets
        {
            get { return this.includeRetweets; }
            set { this.includeRetweets = value; }
        }
        #endregion

        #region ShowAvatars
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_ShowAvatars"),
         DescriptionLocalized("WPPropertyDescription_ShowAvatars")]
        public bool ShowAvatars
        {
            get { return this.showAvatars; }
            set { this.showAvatars = value; }
        }
        #endregion

        #region ShowScreenNames
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_ShowScreenNames"),
         DescriptionLocalized("WPPropertyDescription_ShowScreenNames")]
        public bool ShowScreenNames
        {
            get { return this.showScreenNames; }
            set { this.showScreenNames = value; }
        }
        #endregion

        #region LoadAvatarsUsingSsl
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_TwitterConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_AvatarsWithSsl"),
         DescriptionLocalized("WPPropertyDescription_AvatarsWithSsl")]
        public bool LoadAvatarsUsingSsl
        {
            get { return this.loadAvatarsUsingSsl; }
            set { this.loadAvatarsUsingSsl = value; }
        }
        #endregion
        #endregion

        #region " Cache Properties "
        #region CacheTimespan
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_CacheConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_CacheTimespan"),
         DescriptionLocalized("WPPropertyDescription_CacheTimespan")]
        public int CacheTimespan
        {
            get { return this.cacheTimespan; }
            set { this.cacheTimespan = value; }
        }
        #endregion

        #region ClearCache
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_CacheConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_ClearCache"),
         DescriptionLocalized("WPPropertyDescription_ClearCache")]
        public bool ClearCache 
        {
            get 
            {
                if (this.clearCache)
                {
                    ClearTweetCache();
                    ClearCache = false;
                }
                return this.clearCache; 
            }
            set { this.clearCache = value; }
        }
        #endregion
        #endregion

        #region " Display Properties "
        #region CustomCss
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_DisplayConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_CustomCss"),
         DescriptionLocalized("WPPropertyDescription_CustomCss")]
        public string CustomCss { get; set; }
        #endregion

        #region CustomTemplate
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_DisplayConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_CustomTemplate"),
         DescriptionLocalized("WPPropertyDescription_CustomTemplate")]
        public string CustomTemplate { get; set; }
        #endregion
        #endregion

        #region " Script Properties "
        #region JQueryOnMasterPage
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_ScriptConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_JQueryOnPage"),
         DescriptionLocalized("WPPropertyDescription_JQueryOnPage")]
        public bool JQueryOnMasterPage 
        {
            get { return this.jqueryOnMasterPage; }
            set { this.jqueryOnMasterPage = value; }
        }
        #endregion

        #region JTemplateOnMasterPage
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_ScriptConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_JTemplateOnPage"),
         DescriptionLocalized("WPPropertyDescription_JTemplateOnPage")]
        public bool JTemplateOnMasterPage
        {
            get { return this.jtemplateOnMasterPage; }
            set { this.jtemplateOnMasterPage = value; }
        }
        #endregion

        #region JTimerOnMasterPage
        [WebBrowsable(true),
         CategoryLocalized("WPPropertyCategory_ScriptConfig"),
         Personalizable(PersonalizationScope.Shared),
         WebDisplayNameLocalized("WPPropertyName_JTimerOnPage"),
         DescriptionLocalized("WPPropertyDescription_JTimerOnPage")]
        public bool JTimerOnMasterPage
        {
            get { return this.jtimerOnMasterPage; }
            set { this.jtimerOnMasterPage = value; }
        }
        #endregion
        #endregion
    }
}
